package com.woniuxy.service.Impl;

import com.woniuxy.mapper.SiteMapper;
import com.woniuxy.service.SiteService;
import com.work.framework.pojo.City;
import com.work.framework.pojo.County;
import com.work.framework.pojo.Site;
import com.work.framework.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author Administrator
 * @title: SiteServiceImpl
 * @projectName mashangpai
 * @description: TODO
 * @date 2019/9/1714:42
 */
@Service
public class SiteServiceImpl implements SiteService {

    @Autowired
    private SiteMapper siteMapper;


    @Override
    @Cacheable(value = "Cities")
    public List<City> findCities() {
        List<City> cities = siteMapper.findCities();
        return cities;
    }

    @Override
    @Cacheable(value = "Cities",key = "#cityid")
    public List<County> findCountiesByCityId(Integer cityid) {
        List<County>counties = siteMapper.findCountiesByCityId(cityid);
        return counties;
    }

    @Override
    public Integer addSite(Site site, HttpSession session) {
        User user = (User) session.getAttribute("user");
        site.setUid(user.getUid());
        Integer code = siteMapper.insertSite(site);
        if (code==1){
            return 200;
        }else {
            return 500;
        }
    }

    @Override
    public List<Site> findSites(HttpSession session) {
        User user = (User) session.getAttribute("user");
        List<Site>sites = siteMapper.findSitesByUid(user.getUid());
        return sites;
    }

    @Override
    public Integer deleteSite(Site site, HttpSession session) {
        User user = (User) session.getAttribute("user");
        site.setUid(user.getUid());
        site.setFlage(0);
        Integer code = siteMapper.deleteSite(site);
        if (code == 1){
            return 200;
        }else {
            return 500;
        }
    }

    @Override
    public Integer importSite(Site site, HttpSession session) {
        User user = (User) session.getAttribute("user");
        site.setUid(user.getUid());
        site.setFlage(2);
        siteMapper.update(site.getUid(),1);
        Integer code = siteMapper.deleteSite(site);
        if (code == 1){
            return 200;
        }else {
            return 500;
        }
    }

}
